﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class Level : MonoBehaviour
{
    public Image WarningTex;
    public AudioSource WarningSound;
    public Control m_Control;
    public GameObject BornPoint;
    public GameObject TargetObj;
    public LevelShutPanel ShutPanel;
    public Button SucceedB;
    public Button LoseB;
    public Text MoneyL;
    public FightPanel FightPanel;
    public bool m_IsInLevel = true;
    public AudioSource m_StartSound;
    public AudioSource m_WarningSound;

    public AudioListener BackupListener;
    Ship m_ProtectedTarget;
    protected MineFiled m_MineFiled;
    int m_SpoilsOfWarMoney=0;

    List<Ship> m_EnemyShips = new List<Ship>();
    protected List<Item> m_GetItems = new List<Item>();
    protected List<Item> m_LoseItems = new List<Item>();

    public Level()
    {
        m_MineFiled = new MineFiled_23();
    }
    protected virtual void Start()
    {
        if (!m_IsInLevel)
            return;
        if(m_WarningSound)
            m_WarningSound.Play();
        if (m_StartSound)
            m_StartSound.Play();

        Ship ship = null;
        if(!UtilsGame.IsOnline())
        {
            if (null == m_Control.ControlShp)
            {
                ship = Player.Instance.placeSelectShip(BornPoint.transform.position, BornPoint.transform.rotation);
                m_Control.ControlShp = ship;
            }
            else
            {
                ship = m_Control.ControlShp;
                m_Control.ControlShp = ship;//  出发改变控制船的回调
            }
        }
        m_ProtectedTarget = ship;
        if (m_Control.ControlShp)
            m_Control.ControlShp.OnShipDestory.AddListener(OnShipDestroy);
        m_Control.OnControleShipChanged.AddListener(OnControleShipChanged);

        m_EnemyShips.Clear();
        checkEnemyShips(TargetObj);

        foreach (var ship2 in ShipManager.Instance.m_NoShps)
        {
            Stone stone = ship2 as Stone;
            if (stone)
            {
                stone.InitContent(m_MineFiled);
            }
        }
    }
    void OnControleShipChanged(Ship ship)
    {
        if (ship)
            ship.OnShipDestory.AddListener(OnShipDestroy);
    }

    // Update is called once per frame
    protected virtual void Update()
    {
        if (!m_IsInLevel)
            return;

        CheckComplete();
        if (!m_Control.ControlShp)
            return;
        //  Warning
        if(m_Control.ControlShp.m_ShipStruct.StructurePoint < m_Control.ControlShp.m_ShipStruct.StructurePointMax/2.2)
        {
            float warningValue = 1-(m_Control.ControlShp.m_ShipStruct.StructurePoint / (m_Control.ControlShp.m_ShipStruct.StructurePointMax / 2.2f));
            var color = WarningTex.color;
            color.a = warningValue;

            if(!WarningSound.isPlaying)
            {
                WarningSound.Play();
            }
            WarningTex.color = color; 
            WarningSound.volume = warningValue*0.2f;
        }
        else
        {
            if (WarningSound.isPlaying)
            {
                WarningSound.Stop();
            }
        }
    }

    protected virtual void CheckComplete()
    {
        // Succeed
        if (TargetObj == null)
        {
            OnSucceed();
        }

        //  Lose
        if (m_Control.ControlShp == null)
        {
            OnLose();

        }
    }
    public virtual void OnSucceed()
    {
        ShutPanel.Succeed(m_GetItems);
        enabled = (false);
        if (SucceedB)
            SucceedB.gameObject.SetActive(true);
        if (FightPanel)
            FightPanel.gameObject.SetActive(false);
        MoneyL.text = m_SpoilsOfWarMoney.ToString();

        Player.Instance.Money += m_SpoilsOfWarMoney;

        for (int i = 0; i <m_GetItems.Count; ++i)
        {
            Player.Instance.addItem(m_GetItems[i].m_Type);
        }
        Player.Instance.save();
    }
    protected virtual void OnLose()
    {
        ShutPanel.Lose(m_LoseItems);
        enabled = (false);
        if (LoseB)
            LoseB.gameObject.SetActive(true);
        if (FightPanel)
            FightPanel.gameObject.SetActive(false);
        Player.Instance.save();
    }

    void checkEnemyShips(GameObject obj)
    {
        if(null == obj)
        {
            return;
        }
        Ship ship = obj.GetComponent<Ship>();
        if(ship != null)
        {
            m_EnemyShips.Add(ship);
            ship.OnShipDestory.AddListener(OnShipDestroy);
        }

        for(int i=0; i<obj.transform.childCount; ++i)
        {
            checkEnemyShips(obj.transform.GetChild(i).gameObject);
        }
    }

    protected virtual void OnShipDestroy(Ship ship)
    {
        if(InfluenceMG.get().isHostile("Player", ship.m_ShipStruct.m_Influence))
        {
            float randomValue2 = UnityEngine.Random.Range(0f, 1f);
            for (int i = 0; i < ship.m_WeaponSlots.Count; ++i)
            {
                var weapon = ship.m_WeaponSlots[i].m_Weapon;
                if (null == weapon)
                    continue;

                float randomValue = UnityEngine.Random.Range(0f, 1f);
                if (randomValue < weapon.m_DropRate)
                    m_GetItems.Add(weapon);
            }

            if (randomValue2 < 0.01f)
            {
                switch (ship.m_ShipStruct.m_LicenseLevel)
                {
                    case Driver.DriveLevel.LEVEL_A:
                        m_GetItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_ENGINEA.ToString()));
                        break;
                    case Driver.DriveLevel.LEVEL_B:
                        m_GetItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_ENGINEB.ToString()));
                        break;
                    case Driver.DriveLevel.LEVEL_C:
                        m_GetItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_ENGINEC.ToString()));
                        break;
                    case Driver.DriveLevel.LEVEL_D:
                        m_GetItems.Add(ItemFactory.Instance.createItem(Item.ItemsType.MATERIAL_PROP_ENGINED.ToString()));
                        break;
                }
            }

            m_SpoilsOfWarMoney += UnityEngine.Random.Range((int)(ship.m_ShipStruct.m_Price * 0.01f), (int)(ship.m_ShipStruct.m_Price * 0.1f));
        }
        else if(ship.m_ShipStruct.StructurePoint <=0 &&
            ship.m_ShipStruct.m_Influence == InfluenceMG.INFLUENCE_PLAYER)
        {
            for (int i = 0; i < ship.m_WeaponSlots.Count; ++i)
            {
                if(ship.m_WeaponSlots[i].m_Weapon == null)
                    continue;
                if (UnityEngine.Random.Range(0f, 1f) < ship.m_WeaponSlots[i].m_Weapon.m_DropRate * 0.3f)
                {
                    m_LoseItems.Add(ship.m_WeaponSlots[i].m_Weapon);
                    ship.m_WeaponSlots[i].switchWeapon(null);
                }
            }
            Player.Instance.save();
            BackupListener.enabled = true;
        }
    }

}
